home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / Session.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.9 KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Class representing a TLS session.'''
  5. from utils.compat import *
  6. from mathtls import *
  7. from constants import *
  8.  
  9. class Session:
  10.     '''
  11.     This class represents a TLS session.
  12.  
  13.     TLS distinguishes between connections and sessions.  A new
  14.     handshake creates both a connection and a session.  Data is
  15.     transmitted over the connection.
  16.  
  17.     The session contains a more permanent record of the handshake.  The
  18.     session can be inspected to determine handshake results.  The
  19.     session can also be used to create a new connection through
  20.     "session resumption". If the client and server both support this,
  21.     they can create a new connection based on an old session without
  22.     the overhead of a full handshake.
  23.  
  24.     The session for a L{tlslite.TLSConnection.TLSConnection} can be
  25.     retrieved from the connection\'s \'session\' attribute.
  26.  
  27.     @type srpUsername: str
  28.     @ivar srpUsername: The client\'s SRP username (or None).
  29.  
  30.     @type sharedKeyUsername: str
  31.     @ivar sharedKeyUsername: The client\'s shared-key username (or
  32.     None).
  33.  
  34.     @type clientCertChain: L{tlslite.X509CertChain.X509CertChain} or
  35.     L{cryptoIDlib.CertChain.CertChain}
  36.     @ivar clientCertChain: The client\'s certificate chain (or None).
  37.  
  38.     @type serverCertChain: L{tlslite.X509CertChain.X509CertChain} or
  39.     L{cryptoIDlib.CertChain.CertChain}
  40.     @ivar serverCertChain: The server\'s certificate chain (or None).
  41.     '''
  42.     
  43.     def __init__(self):
  44.         self.masterSecret = createByteArraySequence([])
  45.         self.sessionID = createByteArraySequence([])
  46.         self.cipherSuite = 0
  47.         self.srpUsername = None
  48.         self.sharedKeyUsername = None
  49.         self.clientCertChain = None
  50.         self.serverCertChain = None
  51.         self.resumable = False
  52.         self.sharedKey = False
  53.  
  54.     
  55.     def _clone(self):
  56.         other = Session()
  57.         other.masterSecret = self.masterSecret
  58.         other.sessionID = self.sessionID
  59.         other.cipherSuite = self.cipherSuite
  60.         other.srpUsername = self.srpUsername
  61.         other.sharedKeyUsername = self.sharedKeyUsername
  62.         other.clientCertChain = self.clientCertChain
  63.         other.serverCertChain = self.serverCertChain
  64.         other.resumable = self.resumable
  65.         other.sharedKey = self.sharedKey
  66.         return other
  67.  
  68.     
  69.     def _calcMasterSecret(self, version, premasterSecret, clientRandom, serverRandom):
  70.         if version == (3, 0):
  71.             self.masterSecret = PRF_SSL(premasterSecret, concatArrays(clientRandom, serverRandom), 48)
  72.         elif version in ((3, 1), (3, 2)):
  73.             self.masterSecret = PRF(premasterSecret, 'master secret', concatArrays(clientRandom, serverRandom), 48)
  74.         else:
  75.             raise AssertionError()
  76.         return version == (3, 0)
  77.  
  78.     
  79.     def valid(self):
  80.         '''If this session can be used for session resumption.
  81.  
  82.         @rtype: bool
  83.         @return: If this session can be used for session resumption.
  84.         '''
  85.         if not self.resumable:
  86.             pass
  87.         return self.sharedKey
  88.  
  89.     
  90.     def _setResumable(self, boolean):
  91.         if not self.sharedKey:
  92.             if (not boolean or boolean) and self.sessionID:
  93.                 self.resumable = boolean
  94.             
  95.         
  96.  
  97.     
  98.     def getCipherName(self):
  99.         """Get the name of the cipher used with this connection.
  100.  
  101.         @rtype: str
  102.         @return: The name of the cipher used with this connection.
  103.         Either 'aes128', 'aes256', 'rc4', or '3des'.
  104.         """
  105.         if self.cipherSuite in CipherSuite.aes128Suites:
  106.             return 'aes128'
  107.         if self.cipherSuite in CipherSuite.aes256Suites:
  108.             return 'aes256'
  109.         if self.cipherSuite in CipherSuite.rc4Suites:
  110.             return 'rc4'
  111.         if self.cipherSuite in CipherSuite.tripleDESSuites:
  112.             return '3des'
  113.         return None
  114.  
  115.     
  116.     def _createSharedKey(self, sharedKeyUsername, sharedKey):
  117.         if len(sharedKeyUsername) > 16:
  118.             raise ValueError()
  119.         len(sharedKeyUsername) > 16
  120.         if len(sharedKey) > 47:
  121.             raise ValueError()
  122.         len(sharedKey) > 47
  123.         self.sharedKeyUsername = sharedKeyUsername
  124.         self.sessionID = createByteArrayZeros(16)
  125.         for x in range(len(sharedKeyUsername)):
  126.             self.sessionID[x] = ord(sharedKeyUsername[x])
  127.         
  128.         premasterSecret = createByteArrayZeros(48)
  129.         sharedKey = chr(len(sharedKey)) + sharedKey
  130.         for x in range(48):
  131.             premasterSecret[x] = ord(sharedKey[x % len(sharedKey)])
  132.         
  133.         self.masterSecret = PRF(premasterSecret, 'shared secret', createByteArraySequence([]), 48)
  134.         self.sharedKey = True
  135.         return self
  136.  
  137.  
  138.